﻿<template>
  <div class="refund-manage-container">
    <h2 class="page-title">退款申请</h2>
    
    <!-- 筛选区域 -->
    <div class="filter-section">
      <div class="filter-item">
        <el-input 
          v-model="queryParams.userSearch" 
          placeholder="输入患者姓名或手机号" 
          clearable 
          @keyup.enter="handleQuery"
        />
      </div>
      <div class="filter-item">
        <span class="label">申请时间</span>
        <el-date-picker
          v-model="queryParams.startTime"
          type="datetime"
          placeholder="开始时间"
          value-format="YYYY-MM-DD HH:mm:ss"
          clearable
        />
      </div>
      <div class="filter-item">
        <el-date-picker
          v-model="queryParams.endTime"
          type="datetime"
          placeholder="结束时间"
          value-format="YYYY-MM-DD HH:mm:ss"
          clearable
        />
      </div>
      <div class="filter-item">
        <el-button type="primary" @click="handleQuery">筛选</el-button>
      </div>
    </div>
    
    <!-- 数据表格 -->
    <div class="table-section">
      <!-- 标签页 -->
      <div class="tabs">
        <div 
          v-for="(tab, index) in tabs" 
          :key="index" 
          :class="['tab-item', {'active': queryParams.status === tab.value}]"
          @click="handleTabChange(tab.value)"
        >
          {{ tab.label }}
        </div>
      </div>
      
      <!-- 表格 -->
      <el-table
        v-loading="loading"
        :data="refundList"
        border
        stripe
        style="width: 100%"
      >
        <el-table-column prop="serviceNo" label="服务编号" align="center" />
        <el-table-column label="申请时间" align="center">
          <template #default="scope">
            {{ formatDateTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="userPhone" label="用户" align="center" />
        <el-table-column label="退款金额" align="center">
          <template #default="scope">
            {{ scope.row.refundAmount }} 元
          </template>
        </el-table-column>
        <el-table-column prop="source" label="来源" align="center" />
        <el-table-column label="状态" align="center">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="处理时间" align="center">
          <template #default="scope">
            {{ scope.row.processTime ? formatDateTime(scope.row.processTime) : '-' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120">
          <template #default="scope">
            <el-button type="primary" link @click="handleView(scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="queryParams.pageIndex"
          v-model:page-size="queryParams.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    
    <!-- 查看详情对话框 -->
    <el-dialog v-model="dialogVisible" :title="getDetailTitle()" width="600px" destroy-on-close>
      <div v-if="currentRefund" class="refund-detail">
        <div class="detail-section">
          <h3 class="section-title">订单信息</h3>
          <div class="detail-grid">
            <div class="detail-item">
              <span class="label">服务编号：</span>
              <span>{{ currentRefund.serviceNo }}</span>
            </div>
            <div class="detail-item">
              <span class="label">申请状态：</span>
              <span>{{ getStatusText(currentRefund.status) }}</span>
            </div>
            <div class="detail-item">
              <span class="label">申请时间：</span>
              <span>{{ formatDateTime(currentRefund.createTime) }}</span>
            </div>
            <div class="detail-item">
              <span class="label">用户名：</span>
              <span>{{ currentRefund.userPhone }}</span>
            </div>
            <div class="detail-item">
              <span class="label">订单编号：</span>
              <span>{{ currentRefund.orderNo }}</span>
            </div>
            <div class="detail-item">
              <span class="label">订单金额：</span>
              <span>￥{{ currentRefund.orderAmount }}</span>
            </div>
            <div class="detail-item">
              <span class="label">实际退款金额：</span>
              <span>￥{{ currentRefund.refundAmount }}</span>
            </div>
            <div class="detail-item">
              <span class="label">退款方式：</span>
              <span>{{ currentRefund.refundMethod }}</span>
            </div>
            <div class="detail-item">
              <span class="label">退款类型：</span>
              <span>{{ currentRefund.refundType }}</span>
            </div>
            <div class="detail-item">
              <span class="label">退款原因：</span>
              <span>{{ currentRefund.reason }}</span>
            </div>
          </div>
        </div>

        <!-- 处理信息区域 - 仅已处理或已拒绝时显示 -->
        <div v-if="currentRefund.status === '已处理' || currentRefund.status === '已拒绝'" class="detail-section">
          <h3 class="section-title">处理信息</h3>
          <div class="detail-grid">
            <div class="detail-item">
              <span class="label">处理人员：</span>
              <span>{{ currentRefund.processor || '-' }}</span>
            </div>
            <div class="detail-item">
              <span class="label">处理时间：</span>
              <span>{{ formatDateTime(currentRefund.processTime) }}</span>
            </div>
            <div class="detail-item full-width">
              <span class="label">备注：</span>
              <span>{{ currentRefund.remark || '无' }}</span>
            </div>
          </div>
        </div>

        <!-- 处理备注输入区 - 仅待处理状态显示 -->
        <div v-if="currentRefund.status === '待处理'" class="detail-section">
          <h3 class="section-title">处理备注</h3>
          <el-input
            v-model="remarkContent"
            type="textarea"
            :rows="3"
            placeholder="请输入..."
          />
        </div>
      </div>
      
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">{{ currentRefund && currentRefund.status !== '待处理' ? '关闭' : '返回' }}</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { axiosrzsh } from '@/http/axiosRead'
import { useRouter } from 'vue-router'

const router = useRouter()

// 定义退款申请的接口类型
interface RefundApplication {
  serviceNo: string
  createTime: string
  userPhone: string
  refundAmount: number
  source: string
  status: string
  processTime: string
  orderId: number
  orderNo: string
  // 详情接口额外字段
  orderAmount?: number
  refundMethod?: string
  refundType?: string
  reason?: string
  processor?: string
  remark?: string
}

// 定义查询参数
const queryParams = reactive({
  userSearch: '',
  startTime: '',
  endTime: '',
  status: '0', // 默认查询全部
  pageIndex: 1,
  pageSize: 10
})

// 标签页配置
const tabs = [
  { label: '全部', value: '0' },
  { label: '待处理', value: '1' },
  { label: '已处理', value: '2' },
  { label: '已拒绝', value: '3' }
]

// 数据列表
const refundList = ref<RefundApplication[]>([])
const total = ref(0)
const loading = ref(false)
const dialogVisible = ref(false)
const currentRefund = ref<RefundApplication | null>(null)
const remarkContent = ref('')

// 获取退款申请列表
const getRefundList = async () => {
  loading.value = true
  try {
    const response = await axiosrzsh.get('/RefundApplication', {
      params: queryParams
    })
    if (response.data.code === 200) {
      refundList.value = response.data.pageData || []
      total.value = response.data.totaCount || 0
    } else {
      ElMessage.error(response.data.msg || '获取数据失败')
    }
  } catch (error) {
    console.error('获取退款申请数据失败:', error)
    ElMessage.error('获取数据失败，请检查网络连接')
  } finally {
    loading.value = false
  }
}

// 格式化日期时间
const formatDateTime = (dateTimeStr: string): string => {
  if (!dateTimeStr) return '-'
  const date = new Date(dateTimeStr)
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}:${String(date.getSeconds()).padStart(2, '0')}`
}

// 获取状态显示文本
const getStatusText = (status: string): string => {
  if (status === '1' || status === '待处理') return '待处理'
  if (status === '2' || status === '已处理') return '已处理'
  if (status === '3' || status === '已拒绝') return '已拒绝'
  return status
}

// 获取状态显示类型
const getStatusType = (status: string): string => {
  if (status === '1' || status === '待处理') return 'warning'
  if (status === '2' || status === '已处理') return 'success'
  if (status === '3' || status === '已拒绝') return 'danger'
  return 'info'
}

// 获取详情页面标题
const getDetailTitle = (): string => {
  if (!currentRefund.value) return '退款详情'
  
  const status = currentRefund.value.status
  if (status === '待处理' || status === '1') return '退款详情-待处理'
  if (status === '已处理' || status === '2') return '退款详情-已处理'
  if (status === '已拒绝' || status === '3') return '退款详情-已拒绝'
  
  return '退款详情'
}

// 处理查询
const handleQuery = () => {
  queryParams.pageIndex = 1
  getRefundList()
}

// 处理标签页切换
const handleTabChange = (status: string) => {
  queryParams.status = status
  handleQuery()
}

// 处理分页大小改变
const handleSizeChange = (size: number) => {
  queryParams.pageSize = size
  getRefundList()
}

// 处理分页页码改变
const handleCurrentChange = (page: number) => {
  queryParams.pageIndex = page
  getRefundList()
}

// 处理查看详情
const handleView = async (row: RefundApplication) => {
  try {
    // 假设row.orderId或serviceNo可以作为id使用
    const id = row.orderId || row.serviceNo
    const response = await axiosrzsh.get(`/RefundApplication/${id}`)
    
    if (response.data.code === 200) {
      currentRefund.value = response.data.data
      dialogVisible.value = true
    } else {
      ElMessage.error(response.data.msg || '获取退款详情失败')
    }
  } catch (error) {
    console.error('获取退款详情失败:', error)
    ElMessage.error('获取退款详情失败，请检查网络连接')
  }
}

// 页面加载时获取数据
onMounted(() => {
  getRefundList()
})
</script>

<style scoped>
.refund-manage-container {
  padding: 20px;
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.page-title {
  font-size: 22px;
  font-weight: bold;
  margin-bottom: 20px;
  color: #333;
}

.filter-section {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  margin-bottom: 20px;
  padding: 15px;
  background-color: #f7f7f7;
  border-radius: 5px;
}

.filter-item {
  margin-right: 15px;
  margin-bottom: 10px;
}

.filter-item .label {
  margin-right: 8px;
}

.tabs {
  display: flex;
  border-bottom: 1px solid #eee;
  margin-bottom: 20px;
}

.tab-item {
  padding: 10px 20px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
  border-bottom: 2px solid transparent;
}

.tab-item.active {
  color: #409EFF;
  border-bottom: 2px solid #409EFF;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.refund-detail {
  padding: 10px 0;
}

.section-title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #eaeaea;
}

.detail-section {
  margin-bottom: 20px;
  background-color: #f9f9f9;
  border-radius: 5px;
  padding: 15px;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.detail-item {
  line-height: 24px;
}

.detail-item.full-width {
  grid-column: span 2;
}

.detail-item .label {
  font-weight: bold;
  margin-right: 10px;
  color: #606266;
}
</style>
